//
//  ViewController.m
//  3 - 23锁住
//
//  Created by student on 16/3/23.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
// 卖票
// 100张票
// 3个售票员
@interface ViewController ()

@property (nonatomic,assign) int tickets;

@property (nonatomic,strong) NSLock *lock;//同步锁

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.lock = [[NSLock alloc] init];
    
    self.tickets = 100;
    
    NSThread *sellerThred1 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTicket2) object:nil];
    sellerThred1.name = @"售票员（张三）";
    NSThread *sellerThred2 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTicket2) object:nil];
    sellerThred2.name = @"售票员（李四）";
    NSThread *sellerThred3 = [[NSThread alloc] initWithTarget:self selector:@selector(sellTicket2) object:nil];
    sellerThred3.name = @"售票员（王五）";
    
    [sellerThred1 start];
    [sellerThred2 start];
    [sellerThred3 start];
    
}
#pragma mark 方法1 synchronized
//@synchronized(self)
//{
//    这里面的代码互斥，只有一个线程在运行里面的代码，其他线程运行到这里发现锁住的，会在这里等待
//}

- (void)sellTicket
{
    while (1)
    {
        NSLog(@"%@ 准备卖票",[NSThread currentThread].name);
        NSDate *d1 = [NSDate date];
        @synchronized(self)
        {
            NSDate *d2 = [NSDate date];
            NSTimeInterval t = [d2 timeIntervalSinceDate:d1];
            NSLog(@"%@ 等待了：%.3秒",[NSThread currentThread].name,t);
            //得到当前的余票
            int count = self.tickets;
            
            if(count>0)
            {
                [NSThread sleepForTimeInterval:0.01];
                self.tickets = self.tickets - 1;
                NSLog(@"%@:卖出一张票，剩余%i",[NSThread currentThread].name,self.tickets);
            }
            else
            {
                NSLog(@"票卖光了");
                [NSThread exit];// 线程退出
            }
        }
    }
}

#pragma mark - 方式2 NSLock
// 1 卖票 锁住
// 2 喝口水 不要锁住
// 3 上个厕所 不要锁住
// 4 查询别人是不是正在卖票，如果别人不在买票，你就锁住然后卖票然后解锁，如果别人正在卖票，那你也不要闲着，看看报纸，待会再来查询一下

// 优点:
// 1. 将必须锁住的部分代码锁住,不必须的不锁
// 2.

- (void)sellTicket2
{
    while (1)
    {
        if ([self.lock tryLock])// 尝试去锁住
        {
            // 说明没有人加锁，那我就锁住了
            
            // 开始卖票
            int count = self.tickets;
            if (count>0)
            {
                [NSThread sleepForTimeInterval:0.03];
                self.tickets = self.tickets - 1;
                [self.lock unlock];//已经卖出票了  可以解锁
                NSLog(@"%@:卖出一张票，剩余%i",[NSThread currentThread].name, self.tickets);
                NSLog(@"%@:喝口水休息一下",[NSThread currentThread].name , self.tickets);
                [NSThread sleepForTimeInterval:0.01];
            }
            else
            {
                NSLog(@"%@:票卖光了",[NSThread currentThread].name);
                [self.lock unlock];
                [NSThread exit];// 退出线程
            }
        }
        else
        {
            //没能锁住 别人已锁，先干点别的事情
            NSLog(@"%@:......",[NSThread currentThread].name);
            [NSThread sleepForTimeInterval:0.01];
        }
    }
}















@end
